package main;

//import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
//import java.util.Random;
//import java.util.ArrayList;





//import scenario.*;
//import unit.*;
//import equipment.*;

public class UnitPlacer extends PhaseUI {
	
	public Army unplacedGoodArmy;
	public Army unplacedBadArmy;
	
	public Choice goodPlacers = new Choice();
	public Choice badPlacers = new Choice();
	
	public UnitSelecter myUS;
	
	public PlaceMouse myMouse;
	public MyKey myKey;
	
	public Button done;
	
	public UnitPlacer(UnitSelecter inputUS) {
		myUI = inputUS.myUI;
		myUS = inputUS;
	}
	
	public void beginPhase(){
		if(myUI.game.armiesPlaced == true){
			myUI.beginGame();
			return;
		}
		System.out.println("Placing!");
		UI.messageLog.add("Left click to place selected good Unit, right click for selected bad Unit");
		myUI.repaint();
		MyKey.myUI = this.myUI;
		myMouse = new PlaceMouse();
		myKey = new MyKey();
		myMouse.myUS = this;
		myMouse.myUI = myUI;
		myUI.addMouseListener(myMouse);
		myUI.addKeyListener(myKey);
		
		unplacedGoodArmy = myUS.goodArmy;
		unplacedBadArmy = myUS.badArmy;
		
		myUI.game.armies[0] = new Army(myUI.game);
		myUI.game.armies[1] = new Army(myUI.game);
		
		// Populate placement widgets
		goodPlacers = new Choice();
		goodPlacers.setName("placer");
		goodPlacers.setBounds(0,0, myUI.dim.width/3, 25);
		for(Unit u : unplacedGoodArmy.units){
			goodPlacers.add(u.toChoiceString() + " - " + u.equipmentString());
		}
		badPlacers = new Choice();
		badPlacers.setName("placer");
		badPlacers.setBounds(myUI.dim.width*2/3,0, myUI.dim.width/3,25);
		for(Unit u : unplacedBadArmy.units){
			badPlacers.add(u.toChoiceString() + " - " + u.equipmentString());
		}
		
		done = new Button("Done");
		done.setBounds(myUI.dim.width-100, myUI.dim.height-50, 100, 50);
		done.setName("done");
		done.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				endPhase();
			}
		});
		done.setEnabled(false);
		
		// Add done button and PlaceListener
		myUI.add(done);
		myUI.add(goodPlacers);
		myUI.add(badPlacers);
	}
	
	public void endPhase() {
		myUI.removeByName("done");
		myUI.removeByName("placer");
		
		myUI.removeMouseListener(myMouse);
		myUI.removeKeyListener(myKey);
		myUI.beginGame();
	}
	
	public void paint(Graphics bufferG) {
		if (myUI.game.goodStart != null && myUI.game.badStart != null) {
			Color goodColor = new Color(0, 255, 0, 100);
			
			AffineTransform offsetShift = AffineTransform.getTranslateInstance(-1*myUI.boardOffsetX, -1*myUI.boardOffsetY);
			Area goodClone = (Area)myUI.game.goodStart.clone();
			Area badClone = (Area)myUI.game.badStart.clone();
			
			goodClone.transform(offsetShift);
			badClone.transform(offsetShift);
			
			bufferG.setColor(goodColor);
			((Graphics2D)bufferG).fill(goodClone);
			
			Color badColor = new Color(255,0,0,100);
			bufferG.setColor(badColor);
			((Graphics2D)bufferG).fill(badClone);
			
			bufferG.setColor(Color.BLACK);
		}
	}
}